package com.yzd.otherlearn.jvm;

import java.io.InputStream;

import com.yzd.otherlearn.util.Print;

public class LoadClassTest6 {
	
	public static void main(String[] args) throws Exception {
		ClassLoader myLoader = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException {
				try {
					String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
					InputStream in = getClass().getResourceAsStream(fileName);
					if(in == null) {
						return super.loadClass(name);
					}
					
					byte[] b = new byte[in.available()];
					in.read(b);
					return defineClass(name, b, 0, b.length);
				} catch (Exception e) {
					e.printStackTrace();
					throw new ClassNotFoundException(name);
				}
			}
		};
		
		Object obj = myLoader.loadClass("com.yzd.otherlearn.jvm.LoadClassTest6").newInstance();
		Print.print(obj.getClass());
		Print.print(obj instanceof com.yzd.otherlearn.jvm.LoadClassTest6);
	}
}
